package com.example.animation;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

/**
 * Created by android on 2017/9/7.
 */

public class MoveView extends ImageView {
    public MoveView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }
    private int lastX;
    private int lastY;
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int x = (int) event.getX();
        int y = (int) event.getY();
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                    lastX = x;
                    lastY = y;
                break;
            case MotionEvent.ACTION_MOVE:
                int offsetX = x - lastX;
                int offsetY = y - lastY;
//                layout(getLeft()+offsetX, getTop()+offsetY, getRight()+offsetX , getBottom()+offsetY );//只是改变了位置，属性没有变化,相当于补间动画

                //这种实现的方式相当于属性动画
                RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) getLayoutParams();
                layoutParams.leftMargin = offsetX+getLeft();
                layoutParams.topMargin = offsetY+getTop();
                setLayoutParams(layoutParams);
                break;

            case MotionEvent.ACTION_UP:
                setBackgroundColor(Color.BLACK);
                break;
        }
        return true;
    }
}
